package com.ruicar.afs.cloud.apply.business.service.impl;

import cn.hutool.core.codec.Base32;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.business.mq.config.ArchiveConfig;
import com.ruicar.afs.cloud.apply.business.mq.sender.ArchiveSender;
import com.ruicar.afs.cloud.apply.business.service.ArchiveMQService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @description: 归档接口
 * @author: sijun.yu
 * @date: 2020/8/6 21:18
 */
@Service
@AllArgsConstructor
public class ArchiveMQServiceImpl implements ArchiveMQService {

    private ArchiveSender archiveSender;

    /**
     * 归档推送
     * @return
     */
    public void archivePushMQ(String contractId,String contractNo,String applicationNo,String exchange) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("origin",ArchiveConfig.origin);
        jsonObject.put("contractId",contractId);
        jsonObject.put("contractNo",contractNo);
        jsonObject.put("applicationNo",applicationNo);

        String timestamp = DateUtil.format(DateUtil.date(),DatePattern.PURE_DATETIME_MS_PATTERN);//唯一串
        String mqId = Base32.encode(timestamp, CharsetUtil.UTF_8);;

        JSONObject jsonSend= new JSONObject();
        jsonSend.put("mqId",mqId);
        jsonSend.put("exchange",exchange);
        jsonSend.put("timestamp",timestamp);
        jsonSend.put("data",jsonObject);
        jsonSend.put("remark","");

        if(ArchiveConfig.wworder_refuse.equals(exchange)){
            archiveSender.sendWworderRefuse(jsonSend.toJSONString());
        }else if(ArchiveConfig.wwcontract_discard.equals(exchange)){
            archiveSender.sendWwcontractDiscard(jsonSend.toJSONString());
        }else if(ArchiveConfig.wworder_awaitloansgiveup.equals(exchange)){
            archiveSender.sendWworderAwaitloansgiveup(jsonSend.toJSONString());
        }else if(ArchiveConfig.wwcontract_approved.equals(exchange)){
            archiveSender.sendWwcontractApproved(jsonSend.toJSONString());
        }else if(ArchiveConfig.wwcontract_refuse.equals(exchange)){
            archiveSender.sendWwcontractRefuse(jsonSend.toJSONString());
        }else if(ArchiveConfig.wwcontract_cancel.equals(exchange)){
            archiveSender.sendWwcontractCancel(jsonSend.toJSONString());
        }else if(ArchiveConfig.wwcontract_actived.equals(exchange)){
            archiveSender.sendWwcontractActived(jsonSend.toString());
        }
    }

}
